package com.aio.kgo.simple.form.design.client.impl;

import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.aio.kgo.simple.form.common.domain.bo.ModelDefinitionList;
import com.aio.kgo.simple.form.common.domain.save.ModelDefinitionDo;
import com.aio.kgo.simple.form.database.domain.params.QueryModelParams;
import com.aio.kgo.simple.form.database.service.AioModelService;
import com.aio.kgo.simple.form.design.client.SimpleModelClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class SimpleModelDbClient implements SimpleModelClient {
    @Autowired
    private AioModelService aioModelService;

    @Override
    public <T> void batchFilterModelName(List<T> list, Func1<T, String> modelIdField, Func1<T, String> setModelNameField) {
        if (ObjectUtils.isEmpty(list)){
            return;
        }
        for (T t : list) {
            filterModelName(t,modelIdField,setModelNameField);
        }
    }

    @Override
    public <T> void filterModelName(T object, Func1<T, String> modelIdField, Func1<T, String> setModelNameField) {
        if (ObjectUtil.isNull(object)){
            return;
        }
        String modelIdFieldName = LambdaUtil.getFieldName(modelIdField);
        if (StringUtils.isBlank(modelIdFieldName)){
            return;
        }
        Object modelIdValue = ReflectUtil.getFieldValue(object, modelIdFieldName);
        if (ObjectUtils.isEmpty(modelIdValue)){
            return;
        }
        String setModelNameFieldName = LambdaUtil.getFieldName(setModelNameField);
        if (StringUtils.isBlank(setModelNameFieldName)){
            return;
        }

        String modelName = getModelNameById(modelIdValue.toString());
        if (StringUtils.isNotBlank(modelName)){
            ReflectUtil.setFieldValue(object,setModelNameFieldName,modelName);
        }
    }

    @Override
    public String getModelNameById(String modelId) {
        ModelDefinitionDo modelById = aioModelService.getModelById(modelId);
        if (ObjectUtils.isEmpty(modelById)){
            return null;
        }
        return modelById.getModelName();
    }

    @Override
    public ModelDefinitionDo getModelByKeyAndModule(String modelKey, String module) {
        ModelDefinitionDo modelByKeyAndModule = aioModelService.getModelByKeyAndModule(modelKey, module);
        return modelByKeyAndModule;
    }

    @Override
    public ModelDefinitionList getModelsByModule(String module) {
        QueryModelParams params = new QueryModelParams();
        params.setModule(module);
        List<ModelDefinitionDo> modelList = aioModelService.getModelList(params);
        ModelDefinitionList modelDefinitionList = new ModelDefinitionList(modelList);
        return modelDefinitionList;
    }

    @Override
    public Object getModelDesign(String modelId) {
        ModelDefinitionDo modelById = aioModelService.getModelById(modelId);
        if (ObjectUtils.isEmpty(modelById)){
            log.warn("根据ID获取模型 根据模型ID[ {} ]未能获取到模型定义。",modelId);
            return null;
        }
        return modelById.getModelDesign();
    }
}
